package com.rchat.pocmini.activity.addfriend;

import android.content.Intent;

import com.airtalkee.sdk.AirtalkeeAccount;
import com.aso.common.encrypt.AESUtils;
import com.aso.common.http.bean.BaseBean;
import com.aso.common.http.delegate.HttpCallbackImpl;
import com.rchat.pocmini.R;
import com.rchat.pocmini.activity.qrcode.QRCodePresenterImpl;
import com.rchat.pocmini.bean.ScanUserBean;
import com.rchat.pocmini.bean.SearchUserBean;
import com.rchat.pocmini.bean2eventbus.AddFriendBean;
import com.rchat.pocmini.global.HttpCodeGlobal;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ljd on 2017/8/7.
 */

public class AddFriendPresenterImpl extends AddFriendContract.Presenter {

    private boolean isSearching = false;
    private boolean isScanning = false;


    @Override
    public void onPStart() {
        super.onPStart();
        EventBus.getDefault().register(this);
    }

    /**
     * 搜索用户
     *
     * @param phone
     * @param displayName
     * @param account
     */
    @Override
    void searchUser(String phone, String displayName, String account) {

        if (isSearching) {
            mView.showShortToast(R.string.is_searching_user);
            return;
        }

        if (phone == null || "".equals(phone) ||
                displayName == null || "".equals(displayName) ||
                account == null || "".equals(account)) {
            //phone,displayName,account是一样的
            mView.showShortToast(R.string.username_empty_error);
            return;
        }
        isSearching = true;

        mModel.searchUser(phone, displayName, account, new HttpCallbackImpl<SearchUserBean>() {
            @Override
            public void onStart() {
                mView.showLoading();
            }

            @Override
            public void onSuccess(SearchUserBean bean) {
                if (mView == null) {
                    return;
                }
                isSearching = false;
                mView.showSearchFriendResultDialog(bean);
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                isSearching = false;
                switch (bean.getCode()) {
                    case HttpCodeGlobal.RES_CODE_420 + "":
                        mView.showShortToast(R.string.search_user_no_exist);
                        break;
                    default:
                        mView.showNetError(bean.getCode(), bean.getDesc());
                        break;
                }
            }

            @Override
            public void onFinish() {
                if (null == mView) {
                    return;
                }
                mView.dismissLoading();
            }
        });
    }

    @Override
    void scanUser(final Intent data) {
        String result = data.getStringExtra("scan_result");
        String userName = null;
        if (result == null || "".equals(result)) {
            mView.showShortToast(R.string.qr_code_no_content);
            return;
        }
        try {
            result = AESUtils.getInstance().decrypt(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            JSONObject obj = new JSONObject(result);
            if (!obj.getString("code").equals(QRCodePresenterImpl.QR_STRING_CODE_MY_USERNAME + "")) {
                mView.showShortToast(R.string.no_this_scan_task);
                return;
            }
            userName = obj.getString("userName");
            if (userName.equals(AirtalkeeAccount.getInstance().getUserId())) {
                mView.showShortToast(R.string.can_not_add_self);
                return;
            }
        } catch (JSONException e) {
            mView.showShortToast(R.string.please_scan_this_app_qr_code);
            e.printStackTrace();
            return;
        }

        isScanning = true;
        mModel.scanUser(userName, new HttpCallbackImpl<ScanUserBean>() {
            @Override
            public void onStart() {
                mView.showLoading();
            }

            @Override
            public void onSuccess(ScanUserBean bean) {
                if (mView == null) {
                    return;
                }
                SearchUserBean b = new SearchUserBean();
                SearchUserBean.DataBean.UsersBean usersBean = new SearchUserBean.DataBean.UsersBean();
                List<SearchUserBean.DataBean.UsersBean.UserBean> user = new ArrayList<SearchUserBean.DataBean.UsersBean.UserBean>();
                SearchUserBean.DataBean.UsersBean.UserBean userBean = new SearchUserBean.DataBean.UsersBean.UserBean();
                userBean.setUserName(bean.getData().getUserName());
                userBean.setAccount(bean.getData().getAccount());
                userBean.setDisplayName(bean.getData().getDisplayName());
                user.add(userBean);
                usersBean.setUser(user);
                SearchUserBean.DataBean dataBean = new SearchUserBean.DataBean();
                dataBean.setUsers(usersBean);
                b.setData(dataBean);
                isScanning = false;
                mView.showSearchFriendResultDialog(b);
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                isScanning = false;
            }

            @Override
            public void onFinish() {
                if (null == mView) {
                    return;
                }
                mView.dismissLoading();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusAddFriend(AddFriendBean bean) {

        mModel.addFriend(bean.getUserName(), bean.getOtherName(), new HttpCallbackImpl<BaseBean>() {
            @Override
            public void onStart() {
                mView.showLoading();
            }

            @Override
            public void onSuccess(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                mView.showShortToast(bean.getDesc());
                mView.dismissSearchUserDialog();
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                mView.showShortToast(bean.getDesc());
            }

            @Override
            public void onFinish() {
                if (null == mView) {
                    return;
                }
                mView.dismissLoading();
            }
        });

    }

    @Override
    public void onPDestroy() {
        EventBus.getDefault().unregister(this);
        super.onPDestroy();
    }
}
